package com.sskjdata.cg.ip;

import javax.servlet.http.HttpServletRequest;

/**
 * @Descirption 记录访问ip
 */
public class IpUtil {
    /**
     * @Description 根据request对象获取ip
     * @Author Jason
     * @param request
     * @return java.lang.String
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip!=null&&ip.indexOf(",")!=-1){
            ip=ip.split(",")[0];
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
